package com.company.sendemo.basics.amJavaCollection.javalist;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //需求：属性值都一样就看做是同一个对象
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Student student = (Student) o;
//        return age == student.age && Objects.equals(name, student.name);
//    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

//    //HashSet默认：添加时查找对象的hash值，没有查到就存起来
//    //所以必须让hash值一致才可以
////必须用算法，不然的话，hash值相同时会挂一串
//    public int hashCode() {
//        //return 0;//效率低
////让基本类型*31，引用类型就用自己的hash值
//        final int prime = 31;
//        int result = 1;
//        result = prime * result + age;
//        result = prime * result + ((name == null) ? 0 : name.hashCode());
//        return result;
//
//    }
}
